package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.ServiceConfigurationError;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 处理post请求的中文乱码
		req.setCharacterEncoding("UTF-8");
		//2. 处理响应的字符集编码
		resp.setContentType("text/html;charset=utf8");
		//3. 获取请求参数
		String cmd = req.getParameter("cmd");
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
		}
	}
	//1. 查询学生列表
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.1 查询数据
		List<Student> studentList = studentService.findAll();
		//1.2 将上面的集合放到request作用域
		req.setAttribute("studentList",studentList);
		//1.3 进行页面的跳转
		req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);
	}
}
